package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.MemberDao;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.Ext.Baby;
import cn.sumpu.app.guoguojie.domain.Ext.MemberExt;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class MemberDaoImpl extends SqlSessionDaoSupport implements MemberDao {

	private static final Logger logger = LoggerFactory
			.getLogger(MemberDaoImpl.class);

	public void insert(Member member) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.MemberMapper.insert",
					member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Member> getMemberList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public int getMemberListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberListCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query getMemberListCount failure.", e);
			}
			throw new AppBusinessException("Query getMemberListCount failure.",
					e);
		}
	}

	@Override
	public void updateMember(Member member) throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.MemberMapper.updateMember",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Update member failure.", e);
			}
			throw new AppBusinessException("Update member failure.", e);
		}
	}

	@Override
	public MemberExt getBaseInfo(String id) throws AppBusinessException {
		try {
			return (MemberExt) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.MemberMapper.getBaseInfo",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBaseInfo failure.", e);
			}
			throw new AppBusinessException("getBaseInfo failure.", e);
		}
	}

	@Override
	public void updateHostName(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.MemberMapper.updateHostName",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateHostName failure.", e);
			}
			throw new AppBusinessException("updateHostName failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Baby> getBabyList(String member) throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MemberMapper.getBabyList",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBabyList failure.", e);
			}
			throw new AppBusinessException("getBabyList failure.", e);
		}
	}

	@Override
	public Member getMemberById(String identifier) throws AppBusinessException {

		try {
			return (Member) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberById",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberById failure.", e);
			}
			throw new AppBusinessException("getMemberById failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Member> getMemberListByDoorplate(String doorplate)
			throws AppBusinessException {
		try {
			return (List<Member>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberListByDoorplate",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMemberListByDoorplate failure.", e);
			}
			throw new AppBusinessException("getMemberListByDoorplate failure.",
					e);
		}
	}
	
	//根据标识号删除宝宝信息
	@Override
	public void deleteBabyInfo(String identifier)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.MemberMapper.deleteBabyInfo",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteBabyInfo failure.", e);
			}
			throw new AppBusinessException("deleteBabyInfo failure.", e);
		}
	}

	/**
	 * 根据门牌号取得爸爸妈妈的集合
	 * @param doorplate			门牌号
	 * @throws AppBusinessException
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Member> getParentListByDoorplate(String doorplate)
			throws AppBusinessException {
		try {
			return (List<Member>) getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MemberMapper.getParentListByDoorplate",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getParentListByDoorplate failure.", e);
			}
			throw new AppBusinessException("getParentListByDoorplate failure.",
					e);
		}
	}

	@Override
	public String getHostPwdByDoorplate(String doorplate)
			throws AppBusinessException {
		try {
			return  (String) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.MemberMapper.getHostPwdByDoorplate",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getHostPwdByDoorplate failure.", e);
			}
			throw new AppBusinessException("getHostPwdByDoorplate failure.",
					e);
		}
	}

	@Override
	public Member getHostInfoByDoorplate(String doorplate)
			throws AppBusinessException {
		try {
			return  (Member) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.MemberMapper.getHostInfoByDoorplate",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getHostInfoByDoorplate failure.", e);
			}
			throw new AppBusinessException("getHostInfoByDoorplate failure.",
					e);
		}
	}

}
